/*
Learning from Law Enforcement
Libor Dusek and Christian Traxler

Do-file producing regression results and figures in the paper and (online) appendix

*************************************
*** DESCRIPTION AND PRELIMINARIES
**************************************

*** This do file: RD_01.do
    Runs the key RD analyses at the enforcement cutoff (cutoff 1)
    TOC:
    I.  DEFINE SAMPLE & VARIABLES
    II. RD ANALYSES: Car Level
    III.RD ANALYSES: Ride Level
    IV. RD ANALYSES: Good vs Bad Traffic Conditions
    V.  RD ANALYSES: Heterogeneity
    VI. RD ANALYSES: Placebo Cutoffs
    VII.RD ANALYSES: Bandwidth Sensitivity Analysis

*** Required packages:
    rdrobust, parmest

*** The input dataset:
    Dusek_Traxler_RD_a4.dta
	
*** Output is stored in output directories: OUTPUT/RD_TAB  OUTPUT/RD_FIG , and OUTPUT/RD_temp
	Names of outpupt files correspond to the Table/Figure numbers in the paper and (online) appendix
	
*** List of Figures and Tables produced:
    See Section C of the ReadMe File.
*/

cap clear
set more off

/* Set directories */
global dir C:/yourdir/
cd    $dir
global datadir $dir/datasets/
global outputdir outfiles/
capture mkdir ${outputdir}
capture mkdir ${outputdir}/RD_TAB
capture mkdir ${outputdir}/RD_FIG
capture mkdir ${outputdir}/RD_temp

cap log close
log using    $dir/${outputdir}/log_RD_01.txt, replace

/* Set Global Paraemters */
global A = 4
*Length of Assignment Period, in months (A)
global F = 4
*Length of Follow-Up Period, in months (F)

/* Load Data with an Assignment Period of A months */
use $datadir/Dusek_Traxler_RD_a${A}.dta


/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* I.    DEFINE SAMPLE & VARIABLES
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.A   DEFINE SAMPLE
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Focus on a follow-up (outcome) period of F - months */
rename period_follup_${F} period_follup
drop period_follup_*
rename period_follup period_post
rename dum_haspost_${F} dum_haspost
drop dum_haspost_*
rename period_pre_${F} period_pre
drop period_pre_*

/* We only keep 'relevant' observations during the pre or post [=outcome]
   period */
keep if (period_pre==1 | period_post==1 | dum_trigger==1)

/* Next we focus on observations with a trigger date before the enforcement
   cutoff was adjusted (early July 2017) */
keep if day_trigger<td(1jul2017)

/* Defines assignment speed (S_i in Section 4.1 of the paper, 'speed_Z' here),
   normalized by the specific cutoff k (here: enforcement cutoff at 14km/h above
   the speed limit  */
gen speed_Z = maxspeed - 14

/* focus on sample around Cutoff 1: maxspeed above speed limit and
   below the 2nd cutoff */
keep if speed_Z>-14 & speed_Z<9

/* define first relevant episode (after sample restrictions) */
/* cut sample, focus on first relevant episode */
bysort id: gegen epi_min=min(episode)
keep if episode==epi_min
drop epi_min

/* Identifier for first relevant drive through (pre period; hence: id defined
   independently of whether a ride is observed in the outcome period */
by id: gegen xxid_record_pre_min  = min(id_record) if (period_pre==1 | dum_trigger==1)
by id: gegen id_record_pre_min  = mean(xxid_record_pre_min)
drop xxid_record_*

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.B   PRIMARY OUTCOME AND TREATMENT VARIABLES
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/*TREATMENT RATE: share of rides that happened after being ticketed */
by id:  gegen xxticketed_m = mean(dum_ticketed) if period_post==1
by id: gegen ticketed_m = mean(xxticketed_m)
drop xxticketed*

/*AVERAGE SPEED  (outcome var) */
by id:  gegen xxspeedm_m    = mean(speedm) if period_post==1
by id:  gegen speedm_m    = mean(xxspeedm_m)
drop xxspeedm_*

/*SPEEDING RATE (outcome var), share of rides above speed limit */
by id:  gegen xxspeeding_m     = mean(speeding) if period_post==1
by id:  gegen speeding_m     = mean(xxspeeding_m)
drop xxspeeding_*

/*VIOLATION RATE (outcome var), i.e., rides w/ speed above enforcement cutoff*/
by id:  gegen xxviol_m     = mean(violation) if period_post==1
by id:  gegen violation_m     = mean(xxviol_m)
drop xxviol_*

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.C   COMPLEMENTARY OUTCOME AND TREATMENT VARIABLES
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/*SPEED PERCENTILES */
by id:  gegen xxspeedm_p50  = median(speedm) if period_post==1
by id:  gegen speedm_p50  = mean(xxspeedm_p50)
by id:  gegen xxspeedm_p75  = pctile(speedm) if period_post==1, p(75)
by id:  gegen speedm_p75  = mean(xxspeedm_p75)
by id:  gegen xxspeedm_p90  = pctile(speedm) if period_post==1, p(90)
by id:  gegen speedm_p90  = mean(xxspeedm_p90)
drop xxspeedm_*

/*AVERAGE SPEED - at same and other (= non-trigger) camera zone*/
by id:  gegen xxspeedm_m_same    = mean(speedm) if period_post==1 & rad_trigger==radarid
by id:  gegen speedm_m_same    = mean(xxspeedm_m_same)
by id:  gegen xxspeedm_m_othr    = mean(speedm) if period_post==1 & rad_trigger~=radarid
by id:  gegen speedm_m_othr    = mean(xxspeedm_m_othr)

/*SPEEDING RATE - at same and other (= non-trigger) camera zone*/
by id:  gegen xxspeeding_m_same     = mean(speeding) if period_post==1 & rad_trigger==radarid
by id:  gegen speeding_m_same     = mean(xxspeeding_m_same)
by id:  gegen xxspeeding_m_othr     = mean(speeding) if period_post==1 & rad_trigger~=radarid
by id:  gegen speeding_m_othr     = mean(xxspeeding_m_othr)
drop xxspeeding_*

/* Number of Rides in outcome period */
gen temp=1
by id: gegen xxnrides_post = sum(temp) if period_post==1
by id: gegen nrides_post = mean(xxnrides_post)
replace nrides_post=0 if nrides_post==.
cap drop temp

/* Dummy indicating at least 1 ride in outcome period */
gen     ever_return=1
replace ever_return=0 if nrides_post==0 & nrides_post~=.

/* Average timediff during outcome (!) period */
by id: gegen xxpost_timediff = mean(timediff) if period_post==1
by id: gegen post_timediff = mean(xxpost_timediff)
drop xxpost*

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  I.D  VARS for BALANCING CHECKS
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Number of Rides in PRE(!) treatment period */
gen temp=1
by id: gegen xxnrides_pre = sum(temp) if (period_pre==1 | dum_trigger==1)
by id: gegen   nrides_pre = mean(xxnrides_pre)
drop xxnrides_* temp

/* Driving frequency in pre-treatment (!) period */
gen length_pre = 1 + day_endwait - day_first
gen driving_frequency = nrides_pre/length_pre

/* Log of time diff to next car traveling ahead (measured at entry point of
   a given speed camera) */
gen log_interval=log(timediff)

*time of day I
gen     h_morn=0
replace h_morn=1 if hofday>=5 & hofday<12
gen     h_aftern=0
replace h_aftern=1 if hofday>=12 & hofday<20
gen     h_night=0
replace h_night=1 if hofday>=20 | hofday<5

*time of day II
gen     h_0=0
replace h_0=1 if hofday>=0 & hofday<6
gen     h_6=0
replace h_6=1 if hofday>=6 & hofday<9
gen     h_9=0
replace h_9=1 if hofday>=9 & hofday<12
gen     h_12=0
replace h_12=1 if hofday>=12 & hofday<15
gen     h_15=0
replace h_15=1 if hofday>=15 & hofday<18
gen     h_18=0
replace h_18=1 if hofday>=18 & hofday<21
gen     h_21=0
replace h_21=1 if hofday>=21 & hofday<24

qui tab dofweek, gen(dow_)
qui tab mofyear, gen(month_)

/* Defines Variables for 'Trigger Observation' */
foreach Y of varlist log_interval h_6 h_9 h_12 h_15 h_18 h_21 dow_2 dow_3 dow_4 dow_5 dow_6 dow_7 h_morn h_aftern h_night ///
                    weekend temp_2m wind  month_2 month_3 month_4 month_5 month_6 month_7 month_8 month_9 month_10 month_11 month_12 {
 by id:  gegen xx_m_`Y'_trigger    = mean(`Y') if dum_trigger==1
 by id:  gegen `Y'_T               = mean(xx_m_`Y'_trigger)
 drop xx_m_*
 }
qui compress

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* II. RD ANALYSES, ENFORCEMENT CUTOFF (cut 1) - Car Level Analysis (Main Parts)
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* subsequently defines sample for a given estimation/computation */
gen ESTSAMPLE=0

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.A     Table A.5 : Reduced Form Estimates: de-tour responses?
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Focus: cross-section of cars, even if no observation recorded during
   outcome period ;  Q: Lower driving frequency post-treatment?  */
replace ESTSAMPLE=1 if id_record==id_record_pre_min

rdrobust nrides_post speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su nrides_post if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A5_cut1.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(nrides_post) replace

foreach Y of varlist ever_return  {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 outreg2 using ${outputdir}/RD_TAB/Tab_A5_cut1.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(`Y') append
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Main Analysis Sample: cross-section of cars (=> car level estimates),
  requiring at least one ride in outcome period
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

replace ESTSAMPLE=0
replace ESTSAMPLE=1 if id_record==id_record_pre_min & dum_haspost==1

/* Computed Bins and Binned Means for RD Graphs */
qui gen bin_temp=.
local b = 0.5
forvalues i = -10(`b')9 {
 qui replace bin_temp=`i' if speed_Z>=(`i') & speed_Z<(`i'+`b')
}
gegen bin1=group(bin_temp)
gen  bin2 = bin_temp + `b'/2
drop bin_temp

foreach Y of varlist ticketed_m speeding_m speedm_m  {
 cap drop mean_`Y'
  gegen mean_`Y' = mean(`Y') if ESTSAMPLE==1, by(bin2)
 }

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.B     Table A.4, Panel A : Reduced Form Estimates and
*           Figures 2a, 3a, 3c
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Treated ('TICKETED') */
rdrobust ticketed_m speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su ticketed_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A4_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(ticketed) replace

/* Figure 2a */
twoway  (lpolyci ticketed_m speed_Z if speed_Z<0 & speed_Z>-10 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (lpolyci ticketed_m speed_Z  if speed_Z>0 & speed_Z<9 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (scatter mean_ticketed_m bin2 if inrange(speed_Z,-10,10), mcolor(navy) msize(medsmall)), xline(0, lcolor(gs0) lpattern(dash)) legend(off) ///
        xlabel(-10(2.5)10) xtitle("Assignment Speed") ytitle("Treated Rides") graphregion(color(white)) plotregion(color(white))
 graph export ${outputdir}/RD_FIG/Fig_2a_ticketed.pdf, replace as(pdf)

/* Speeding */
rdrobust speeding_m speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A4_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) append

/* Figure 3a */
twoway  (lpolyci speeding_m speed_Z if speed_Z<0 & speed_Z>-10 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
    (lpolyci speeding_m speed_Z  if speed_Z>0 & speed_Z<9 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
    (scatter mean_speeding_m bin2 if inrange(speed_Z,-10,10), mcolor(navy) msize(medsmall)), xline(0, lcolor(gs0) lpattern(dash)) legend(off) ///
    xlabel(-10(2.5)10) xtitle("Assignment Speed") ytitle("Speeding Rate") graphregion(color(white)) plotregion(color(white))
 graph export ${outputdir}/RD_FIG/Fig_3a.pdf, replace as(pdf)

/* (Re-)Offending */
rdrobust violation_m speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su violation_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A4_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(offense) append

/* Mean Speed */
rdrobust speedm_m speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A4_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed) append

/* Figure 3c */
twoway  (lpolyci speedm_m speed_Z if speed_Z<0 & speed_Z>-10 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (lpolyci speedm_m speed_Z  if speed_Z>0 & speed_Z<9 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(navy) alwidth(none) acolor(gs12) kernel(tri)) ///
        (scatter mean_speedm_m bin2 if inrange(speed_Z,-10,10), mcolor(navy) msize(medsmall)), xline(0, lcolor(gs0) lpattern(dash)) legend(off) ///
        xlabel(-10(2.5)10) xtitle("Assignment Speed") ytitle("Mean Speed") graphregion(color(white)) plotregion(color(white))
 graph export ${outputdir}/RD_FIG/Fig_3c.pdf, replace as(pdf)

/* 'Top Speed' (p90) */
rdrobust speedm_p90 speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
 qui su speedm_p90 if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A4_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed p90) append


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.C     Data for bandwidth sensitivity check
            => see Section VII (below) in this do file
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* RANGE for Bandwidth Sensitivity Checks;  Note 1: constant factor (1.4) used
   for bias correction. Note 2: all based on reduced form estimates; (Wald
   Estimates would vary via first-stage, too).
*/

forvalues H = 0.5(0.25)8 {
 local B = 1.4*`H'
 local H2=100*`H'
 qui rdrobust speeding_m  speed_Z if ESTSAMPLE==1, all p(1) h(`H') b(`B') vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/RedF_Cut1_speeding_BW_`H2'.dta,replace)

 qui rdrobust speedm_m  speed_Z if ESTSAMPLE==1, all p(1) h(`H') b(`B') vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/RedF_Cut1_speed_BW_`H2'.dta, replace)
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.D     Balance/Smoothness of Pre-Treatment Vars: Table A2
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Tables A2, Column 1 */
rdrobust driving_frequency  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 qui su driving_frequency if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A2_Row-1.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(driving_frequ) replace

/* Tables A2, Columns 2--10 */
foreach Y of varlist nrides_pre plate_A plate_S log_interval_T temp_2m_T wind_T  h_6 h_9 h_12 {
 qui rdrobust `Y'  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  qui outreg2 using ${outputdir}/RD_TAB/Tab_A2_Row-1.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(`Y') append
}

/* Tables A2, Column 11 */
rdrobust h_15  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 qui su h_15 if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A2_Row-2.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(h_15) replace

/* Tables A2, Columns 12--20 */
foreach Y of varlist h_18 h_21  dow_2_T dow_3_T dow_4_T dow_5_T dow_6_T dow_7_T month_2_T {
 qui rdrobust `Y'  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  qui outreg2 using ${outputdir}/RD_TAB/Tab_A2_Row-2.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(`Y') append
}

/* Tables A2, Column 21 */
rdrobust month_3_T  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 qui su month_3_T if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
 local y_l = r(mean)
 qui outreg2 using ${outputdir}/RD_TAB/Tab_A2_Row-3.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(month_3_T) replace

/* Tables A2, Columns 22--30 */
foreach Y of varlist month_4_T month_5_T month_6_T month_7_T month_8_T month_9_T month_10_T month_11_T month_12_T {
 qui rdrobust `Y'  speed_Z if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  qui outreg2 using ${outputdir}/RD_TAB/Tab_A2_Row-3.tex, tex(frag) se br addstat(Y_L, `y_l') adec(3) sdec(4) bdec(4) ctitle(`Y') append
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.E     Balance/Smoothness of Pre-Treatment Vars: Figure A5
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

cap drop weekend
rename weekend_T weekend
rename h_night_T night
rename h_morn_T morning
rename h_aftern_T afternoon
rename driving_frequency driving_frequ
rename nrides_pre rides_pre
rename log_interval_T traffic
rename plate_S regional_nplate
rename plate_A prague_nplate

foreach Y of varlist rides_pre driving_frequ traffic prague_nplate regional_nplate weekend morning afternoon night {
cap drop mean_`Y'
 gegen mean_`Y' = mean(`Y') if ESTSAMPLE==1, by(bin2)
qui rdbwselect `Y' speed_Z if ESTSAMPLE==1, p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
twoway  (lpolyci `Y' speed_Z if speed_Z<0 & speed_Z>-10 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(gs0) alwidth(none) acolor(gs13) kernel(tri)) ///
        (lpolyci `Y' speed_Z  if speed_Z>0 & speed_Z<9 & ESTSAMPLE==1, bw(`bw') deg(1) clcolor(gs0) alwidth(none) acolor(gs13) kernel(tri)) ///
        (scatter mean_`Y' bin2 if inrange(speed_Z,-10,10), msymbol(Oh) mcolor(gs2) msize(medsmall)), xline(0, lcolor(gs0) lpattern(dash)) legend(off) ///
        xlabel(-10(2.5)10) xtitle("Assignment Speed") ytitle("") graphregion(color(white)) plotregion(color(white))  title("`Y'")
  graph export ${outputdir}/RD_FIG/Fig_A5_`Y'.pdf, replace as(pdf)
 cap drop mean_`Y'
}

rename regional_nplate  plate_S
rename prague_nplate    plate_A

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.F     Table 2, Panel A : Main Wald Estimates, Car Level
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Speeding */
rdrobust speeding_m  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) replace

/* (Re-) Offending */
rdrobust violation_m  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su violation_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(offense) append

/* Mean speed */
rdrobust speedm_m  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speedm) append

/* Table 2, Panel A, Columns 4 -- 6 */
foreach Y of varlist              speedm_p50 speedm_p75 speedm_p90 {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  local bw = h_mserd
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  II.G     Table 3, Panel A : Same vs Other Camera Zones, Car Level
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust speeding_m_same speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m)  all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m_same if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/same) replace

rdrobust speeding_m_othr speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m_othr if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/other) append

rdrobust speeding_m_same speed_Z if ESTSAMPLE==1 & speeding_m_othr~=., fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m_same if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & speeding_m_othr~=.
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/same) append

rdrobust speeding_m_othr speed_Z if ESTSAMPLE==1 & speeding_m_same~=., fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m_othr if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & speeding_m_same~=.
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/other) append

rdrobust speedm_m_same speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m_same if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/same) append

rdrobust speedm_m_othr speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m_othr if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/other) append

rdrobust speedm_m_same speed_Z if ESTSAMPLE==1 & speedm_m_othr~=., fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m_same if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & speeding_m_othr~=.
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/same) append

rdrobust speedm_m_othr speed_Z if ESTSAMPLE==1 & speedm_m_same~=., fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m_othr if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & speeding_m_same~=.
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/other) append

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* III. RD ANALYSES, ENFORCEMENT CUTOFF (cut 1) - Ride Level Analysis
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* Different Sample => RIDE LEVEL Estimates: all rides in outcome period */
replace ESTSAMPLE=0
replace ESTSAMPLE=1 if dum_haspost==1 & period_post==1

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  III.A     Table 2, Panel B : Main Wald Estimates, Ride Level
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust speeding  speed_Z if ESTSAMPLE==1, fuzzy(dum_ticketed) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding) replace

foreach Y of varlist violation speedm  {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1, fuzzy(dum_ticketed) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
  local bw = h_mserd

  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_2_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  III.B     Table 3, Panel B : Same vs Other Camera Zones, Ride Level
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Count of rides at Same/Other camera zones (in outcome period */
gen temp=1
bysort id: gegen xxnrides_post_same = sum(temp) if period_post==1 & rad_trigger==radarid
by id:     gegen nrides_same = mean(xxnrides_post_same)
replace nrides_same=0 if nrides_same==.
by id: gegen xxnrides_post_othr = sum(temp) if period_post==1 & radarid~=rad_trigger
by id:     gegen nrides_othr = mean(xxnrides_post_othr)
replace nrides_othr=0 if nrides_othr==.
drop xxnrides* temp
qui compress

gen     same_camera=0
replace same_camera=1 if rad_trigger==radarid

/* Speeding (Col 1 -- 4) */
rdrobust speeding speed_Z                     if ESTSAMPLE==1 & same_camera==1, fuzzy(dum_ticketed)  all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==1
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/same) replace

rdrobust speeding speed_Z                     if ESTSAMPLE==1 & same_camera==0, fuzzy(dum_ticketed) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/other) append

rdrobust speeding speed_Z                     if ESTSAMPLE==1 & same_camera==1 & nrides_othr>0, fuzzy(dum_ticketed) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==1 & nrides_othr>0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/same) append

rdrobust speeding speed_Z                     if ESTSAMPLE==1 & same_camera==0 & nrides_same>0, fuzzy(dum_ticketed) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speeding if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==0 & nrides_same>0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding/other) append

/* Speed (Col 5 -- 8) */
rdrobust speedm speed_Z                     if ESTSAMPLE==1 & same_camera==1, fuzzy(dum_ticketed) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speedm if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==1
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/same) append

rdrobust speedm speed_Z                     if ESTSAMPLE==1 & same_camera==0, fuzzy(dum_ticketed) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speedm if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/other) append

rdrobust speedm speed_Z                     if ESTSAMPLE==1 & same_camera==1 & nrides_othr>0, fuzzy(dum_ticketed) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speedm if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==1 & nrides_othr>0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/same) append

rdrobust speedm speed_Z                     if ESTSAMPLE==1 & same_camera==0 & nrides_same>0, fuzzy(dum_ticketed) all p(1) bwselect(mserd) vce(cluster id) kernel(tri)
 local bw = h_mserd
  qui su speedm if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & same_camera==0 & nrides_same>0
  local y_l = `r(mean)'
  local rel = 100 * e(tau_bc)/`r(mean)'
 qui outreg2 using ${outputdir}/RD_TAB/Tab_3_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed/other) append

drop nrides_same nrides_othr

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  III.C     Fig 4a : Shift in speed distribution
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

gen relspeedm = speedm - speedlimit

twoway (kdensity relspeedm if  speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1, clcolor(dkgreen) lwidth(medium) xline(0 14, lcolor(gs8) lpattern(longdash) lwidth(vthin) ) ///
        legend(off) range(-22.5 22.5)  xlabel(-20(10)20) xtitle("Speed (relative to limit)") ytitle("Density") graphregion(color(white)) plotregion(color(white)) ) ///
        (kdensity relspeedm if  speed_Z>0 & speed_Z<0.5 & ESTSAMPLE==1, range(-22.5 22.5)  clcolor(red) lpattern(dash) lwidth(medium))
 graph export ${outputdir}/RD_FIG/Fig_4a_distribution.pdf, replace as(pdf)

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  III.D     Fig A7 : Relative change in speed distribution
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
count
preserve

/* Speed Brackets for Bar Graphs */
qui gen speed_bin_temp=.
*bin size
local b = 7
local bmax = 21 - `b'
forvalues i = -14(`b')`bmax' {
 qui replace speed_bin_temp=`i' if relspeedm>=(`i') & relspeedm<(`i'+`b')
}
gegen sbin1=group(speed_bin_temp)
gen  sbin2 = speed_bin_temp + `b'/2
drop speed_bin_temp

/* "Treatment": barely below/above enforcement cutoff */
gen     treated_temp=0 if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
replace treated_temp=1 if speed_Z>0 & speed_Z< 0.5 & ESTSAMPLE==1

/* Restrict sample & Count obs */
keep if treated_temp~=.
cap drop temp
gen temp=1-treated
gegen N_treated=sum(treated_temp)
gegen N_untreated=sum(temp)
drop temp

/* Collapse at Speed Bracket / Treatment Group level */
gen temp=1
collapse (sum) temp (mean) N_untreated N_treated, by(treated sbin2)
bysort sbin2: gegen N_bin_untreated=sum(temp) if treated==0
sort sbin2 treated
replace N_bin_untreated=N_bin_untreated[_n-1] if sbin2==sbin2[_n-1]

/* Normalization (see Figure Note) */
gen scaling_factor=N_treated/N_untreated
gen     scaled_obs=temp if treated==0
replace scaled_obs=temp/scaling_factor if treated==1
gen level_diff_scaled=scaled_obs-scaled_obs[_n-1] if sbin2==sbin2[_n-1]
gen rate4_diff_sc=level_diff_sc/N_bin_untreated

gen     bin_group=0 if sbin2==-10.5
replace bin_group=1 if sbin2==-3.5
replace bin_group=2 if sbin2== 3.5
replace bin_group=3 if sbin2==10.5
replace bin_group=4 if sbin2==17.5

label define l_bins 0 "{&minus}14  -  {&minus}7" 1 "{&minus}7  -  0" 2 "0  -  7" 3 "7  - 14"  4 "14 - 21"
label values bin_group l_bins

graph bar rate4_diff_sc, over(bin_group)  ytitle("Relative Change in Distrib.")
 graph export ${outputdir}/RD_FIG/Fig_A7.pdf, replace

restore

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* IV. RD ANALYSES, ENFORCEMENT CUTOFF (cut 1) - Good vs Bad driving Conditions
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* Sample: cross-section of cars w/ at least one ride in outcome period */

replace ESTSAMPLE=0
replace ESTSAMPLE=1 if id_record==id_record_pre_min & dum_haspost==1

/* Indicator for relevant Cross Section of Cars */
cap drop SAMPLE_CARS
gen     xxsample_cars=0
replace xxsample_cars=1 if ESTSAMPLE==1
gegen SAMPLE_CARS=max(xxsample_cars), by(id)
drop xxsample_*

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.A   DEFINES COMPLEMENATARY VARIABLES - Main Analysis
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* defines good driving conditions */
su timediff if SAMPLE_CARS==1 & period_post==1, d

gen      good_conditions = 0 if timediff<=r(p50) & timediff~=. & SAMPLE_CARS==1  & period_post==1
replace  good_conditions = 1 if  timediff>r(p50) & timediff~=. & SAMPLE_CARS==1  & period_post==1

/* Outcome: SPEEDING RATE in outcoe period, for good/bad conditions */
by id:  gegen xxspeeding_good     = mean(speeding) if period_post==1 & good_conditions==1
by id:  gegen speeding_good     = mean(xxspeeding_good)
by id:  gegen xxspeeding_bad     = mean(speeding) if period_post==1 & good_conditions==0
by id:  gegen speeding_bad     = mean(xxspeeding_bad)
drop    xxspeeding_*

/* Outcome: AVERAGE SPEED in outcome period, for good/bad conditions */
by id:  gegen xxspeedm_good    = mean(speedm) if period_post==1  & good_conditions==1
by id:  gegen speedm_good    = mean(xxspeedm_good)
by id:  gegen xxspeedm_bad    = mean(speedm) if period_post==1  & good_conditions==0
by id:  gegen speedm_bad    = mean(xxspeedm_bad)
drop xxspeedm_*

/* Outcome: "Top SPEED" (p90) in outcome period, for good/bad conditions */
by id:  gegen xxspeedm_p90_good    = pctile(speedm) if period_post==1  & good_conditions==1, p(90)
by id:  gegen speedm_p90_good    = mean(xxspeedm_p90_good)
by id:  gegen xxspeedm_p90_bad    = pctile(speedm) if period_post==1  & good_conditions==0, p(90)
by id:  gegen speedm_p90_bad    = mean(xxspeedm_p90_bad)
drop xxspeedm_*

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.B   Table A.7, Panel A: Wald estiamtes for good/bad traffic conditions
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust speeding_good  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding-good) replace

rdrobust speedm_good  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-good) append

rdrobust speedm_p90_good  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_p90_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-good) append

rdrobust speeding_bad  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding-bad) append

rdrobust speedm_bad   speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-bad) append

rdrobust speedm_p90_bad   speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_p90_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-bad) append

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.C   Table A.7, Panel B: Wald estiamtes for good/bad traffic conditions,
*        for a constant set of cars (observed under good and bad conditions)
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

replace speeding_good   = . if ESTSAMPLE==1 & speeding_bad==.
replace speedm_good   = . if ESTSAMPLE==1 & speedm_bad==.
replace speedm_p90_good   = . if ESTSAMPLE==1 & speedm_p90_bad==.

replace speeding_bad   = . if ESTSAMPLE==1 & speeding_good==.
replace speedm_bad   = . if ESTSAMPLE==1 & speedm_good==.
replace speedm_p90_bad   = . if ESTSAMPLE==1 & speedm_p90_good==.

rdrobust speeding_good  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding-good) replace

rdrobust speedm_good  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-good) append

rdrobust speedm_p90_good  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_p90_good if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-good) append

rdrobust speeding_bad  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding-bad) append

rdrobust speedm_bad   speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-bad) append

rdrobust speedm_p90_bad   speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_p90_bad if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_A7_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(relspeed-bad) append

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.D   DEFINES COMPLEMENATARY VARIABLES - Selection Analysis
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

preserve
keep if SAMPLE_CARS==1

/*GOOD-CONDITION RIDES, Share */
by id:  gegen xxgood_rides   = mean(good_conditions) if period_post==1
by id:  gegen good_rides     = mean(xxgood_rides)
drop xxgood_rides

/*GOOD- /BAD-CONDITION RIDES, Counts */
gegen xxgood_rides_N   = sum(good_conditions) if period_post==1, by(id)
gegen good_rides_N     = mean(xxgood_rides_N), by(id)
drop xxgood_rides_N

gen bad_conditions = 1 - good_conditions
gegen xxbad_rides_N   = sum(bad_conditions) if period_post==1, by(id)
gegen bad_rides_N     = mean(xxbad_rides_N), by(id)
drop xxbad_rides_N bad_conditions

/* Average Traffic Density => log(time-diff) */
by id:  gegen xxtraffic_dens     = mean(timediff) if period_post==1
by id:  gegen traffic_dens     = mean(xxtraffic_dens)
drop xxtraffic_*
gen log_timdiff_m =log(traffic_dens)

/* Average Hour-of-day (numeric), outcome(!) period */
by id:  gegen xxhour_post     = mean(hofday) if period_post==1
by id:  gegen hour_post     = mean(xxhour_post)
drop xxhour_*

/* Share of Morning / Night rides, outcome(!) period */
cap drop h_6
gen     h_6=0
replace h_6=1 if hofday>=6 & hofday<9
by id:  gegen xxhour_morn     = mean(h_6) if period_post==1
by id:  gegen hour_morn     = mean(xxhour_morn)
drop h_6

cap drop h_night
gen     h_night=0
replace h_night=1 if hofday>=20 | hofday<5
by id:  gegen xxhour_night     = mean(h_night) if period_post==1
by id:  gegen hour_night     = mean(xxhour_night)
drop xxhour_* h_night

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.E      Table C.1, Panel A : Selection into driving conditions
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust log_timdiff_m  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su log_timdiff_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C1_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(log_timdiff_m) replace

foreach Y of varlist good_rides_N bad_rides_N good_rides  hour_post hour_morn hour_night {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  local bw = h_mserd
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C1_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IV.E      Table C.1, Panel B : Selection; constrained sample
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

rdrobust log_timdiff_m  speed_Z if ESTSAMPLE==1 & bad_rides_N>0 & good_rides_N>0, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su log_timdiff_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & bad_rides_N>0 & good_rides_N>0
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C1_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(log_timdiff_m) replace

foreach Y of varlist good_rides_N bad_rides_N good_rides  hour_post hour_morn hour_night {
 qui rdrobust `Y' speed_Z if ESTSAMPLE==1 & bad_rides_N>0 & good_rides_N>0, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  local bw = h_mserd
  qui su `Y' if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & bad_rides_N>0 & good_rides_N>0
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C1_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle({`Y'}) append
}

restore

*/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
** V.   RD ANALYSES, ENFORCEMENT CUTOFF (cut 1) - Heterogeneity (Tab C.4)
** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* Sample: cross-section of cars w/ at least one outcome period ride */

replace ESTSAMPLE=0
replace ESTSAMPLE=1 if id_record==id_record_pre_min & dum_haspost==1

qui su driving_frequ      if ESTSAMPLE==1,d
qui gen      frequent_driver=0 if driving_frequ<=r(p50) & ESTSAMPLE==1
qui replace  frequent_driver=1 if driving_frequ> r(p50) & ESTSAMPLE==1 & driving_frequ~=.

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* V.A   Table C.4, Panel A : Split Sample, Outcome: Speeding
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Frequent vs infrequent drivers */
rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & frequent_driver==0, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & frequent_driver==0
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding, infrequent) replace

rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & frequent_driver==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & frequent_driver==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding, frequent) append

/* Number plate regions */
rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & plate_S==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_S==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding, "Local") append

rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & plate_A==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_A==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding, Prague) append

rdrobust speeding_m  speed_Z if ESTSAMPLE==1 & plate_O==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_O==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-A.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speeding, Other) append

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* V.B   Table C.4, Panel B : Split Sample, Outcome: Mean Speed
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Frequent vs infrequent drivers */
rdrobust speedm_m  speed_Z if ESTSAMPLE==1 & frequent_driver==0, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & frequent_driver==0
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed, infrequent) replace

rdrobust speedm_m  speed_Z if ESTSAMPLE==1 & frequent_driver==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & frequent_driver==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed, frequent) append

/* Number plate regions */
rdrobust speedm_m  speed_Z if ESTSAMPLE==1 & plate_S==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_S==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed, "Local") append

rdrobust speedm_m  speed_Z if ESTSAMPLE==1 & plate_A==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_A==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed, Prague) append

rdrobust speedm_m  speed_Z if ESTSAMPLE==1 & plate_O==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
 local bw = h_mserd
  qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1 & plate_O==1
  local y_l = r(mean)
  local rel = 100 * e(tau_bc)/`y_l'
 outreg2 using ${outputdir}/RD_TAB/Tab_C4_Panel-B.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(speed, Other) append


**/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
** VI.   RD ANALYSIS: Placebo Cutoffs (around cutoff 1)
** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

set seed 918273
keep if ESTSAMPLE==1

*/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* VI.A      Reduced Form Estimates at Placebo Cutoffs
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

gen speed_Z_random = .

/* First: shift cutoff 1kmh `to left', randomly add/substract random var1 [0,1]
   and run reduced form; store results. */
local t = 1
while `t' <= 500 {
 local random1 = runiform()
 local random2 = runiform()
 qui replace speed_Z_random = speed_Z - 1 + `random1' if `random2'<0.5
 qui replace speed_Z_random = speed_Z - 1 - `random1' if `random2'>0.5

 qui rdrobust speeding_m  speed_Z_random if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/placebo_cut1_speeding_`t', replace)
 qui rdrobust speedm_m  speed_Z_random if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/placebo_cut1_speed_`t', replace)
 local t = `t' + 1
}

/* Second: shift cutoff 1kmh `to right', and repeat same steps as above */
local t = 501
while `t' <= 1000 {
 local random1 = runiform()
 local random2 = runiform()
 qui replace speed_Z_random = speed_Z + 1 + `random1' if `random2'<0.5
 qui replace speed_Z_random = speed_Z + 1 - `random1' if `random2'>0.5

 qui rdrobust speeding_m  speed_Z_random if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/placebo_cut1_speeding_`t', replace)
 qui rdrobust speedm_m  speed_Z_random if ESTSAMPLE==1, all p(1) bwselect(mserd) vce(hc0) kernel(tri)
  qui parmest, saving(${outputdir}/RD_temp/placebo_cut1_speed_`t', replace)
 local t = `t' + 1
}

*/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* VI.B      Figure A.10 : Plot CDF of placebo estiamates
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/* Figure A.10, Subfig. (a) */
cap clear
use ${outputdir}/RD_temp/placebo_cut1_speeding_1.dta
forvalues t = 2/1000 {
 append using ${outputdir}/RD_temp/placebo_cut1_speeding_`t'.dta
}
keep if parm=="Robust"
keep estimate
cumul estimate, gen(cdf_base)

twoway scatter cdf_base estimate, sort msymbol(circle_hollow) msize(small) xline(0 -0.0812)  ylabel(0 "0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1 "1", angle(horizontal)) ///
    xlabel(-0.1 "-0.10" -0.05 "-0.05" 0.00 "0.00" 0.05 "0.05" 0.1 "0.10" , angle(horizontal)) ytitle("cumulative distribution") xtitle("Reduced Form Estimates: Speeding") text(0.2 0.06 " ")
 graph export ${outputdir}/RD_FIG/Fig_A10_a_placebo_speeding.pdf, as(pdf) replace

count if estimate<-0.0812


/* Figure A.10, Subfig. (c) */
cap clear

use ${outputdir}/RD_temp/placebo_cut1_speed_1.dta
forvalues t = 2/1000 {
 append using ${outputdir}/RD_temp/placebo_cut1_speed_`t'.dta
}
keep if parm=="Robust"
keep estimate
cumul estimate, gen(cdf_base)

twoway scatter cdf_base estimate, sort msymbol(circle_hollow) msize(small) xline(0  -1.3512)  ylabel(0 "0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1 "1", angle(horizontal)) ///
    xlabel(-1.5 "-1.5" -1 "-1.0"  -0.5 "-0.5"  0.00 "0.00" 0.5 "0.5" 1 "1.0" 1.5 "1.5" , angle(horizontal)) ytitle("cumulative distribution") xtitle("Reduced Form Estimates: Speed") text(0.2 0.06 " ")
 graph export ${outputdir}/RD_FIG/Fig_A10_c_placebo_speed.pdf, as(pdf) replace

count if estimate<-1.3512
clear

*/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
** VII.   RD ANALYSES, ENFORCEMENT CUTOFF - Bandwidth Sensitivity (Fig A.8)
** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
** +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/* Work with data created in Section II.C (see above): speeding outcome  */
cap clear
use ${outputdir}/RD_temp/RedF_Cut1_speeding_BW_50.dta, clear
 gen bw = 0.5

forvalues H = 0.75(0.25)8 {
 local h = `H'*100
 append using ${outputdir}/RD_temp/RedF_Cut1_speeding_BW_`h'.dta
 replace bw = `H' if bw ==.
 }

/* keeping only the "Robust" estimator */
keep if parm=="Robust"
drop parm

label var min95 "Lower 95% CI"
label var max95 "Upper 95% CI"
label var bw "Bandwidth"
label var estimate "Estimate"

*/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* VII.A   Figure A.8, Subfigure (a)
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

twoway (line estimate bw, lw(medthick) lcolor(navy)) (line min95 bw, lpattern(longdash_dot) lcolor(maroon)) (line max95 bw,lpattern(longdash_dot) lcolor(maroon)), ///
       xtitle("Bandwidth") xlabel(0.5(0.5)8) ylabel(-0.2(0.05)-0) ytitle("Speeding, enforcement cutoff") legend(off) graphregion(color(white))
 graph export ${outputdir}/RD_FIG/Fig_A8_a_Bandwidth_speeding.pdf,replace


cap clear
/* Work with data created in Section II.C (see above): speed outcome */
use ${outputdir}/RD_temp/RedF_Cut1_speed_BW_50.dta, clear
 gen bw = 0.5

forvalues H = 0.75(0.25)8 {
 local h = `H'*100
 append using ${outputdir}/RD_temp/RedF_Cut1_speed_BW_`h'.dta
 replace bw = `H' if bw ==.
 }

keep if parm=="Robust"
drop parm

label var min95 "Lower 95% CI"
label var max95 "Upper 95% CI"
label var bw "Bandwidth"
label var estimate "Estimate"

*/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* VII.B   Figure A.8, Subfigure (c)
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

twoway (line estimate bw, lw(medthick) lcolor(navy)) (line min95 bw, lpattern(longdash_dot) lcolor(maroon)) (line max95 bw,lpattern(longdash_dot) lcolor(maroon)), ///
        xtitle("Bandwidth") xlabel(0.5(0.5)8) ylabel(-3(0.5)-0) ytitle("Speed, enforcement cutoff") legend(off) graphregion(color(white))
 graph export ${outputdir}/RD_FIG/Fig_A8_c_Bandwidth_speed.pdf,replace

clear



log close
